<?php
include_once "User.php";
	class Client extends User
	{
		public $Email;
		public $DateOfBirth;
		public $Street;
		public $Complement;
		public $District;
		public $City;
		public $State;
		public $Country;
		public $ZipCode;	
		public $Fone;
		public $Cel;
		public $Password;
		public $Login;
		
		
		public static function cadastraClient($var_cpf, $var_password, $var_login, $var_name, $var_sex, $var_email,
		$var_date, $var_adress, $var_complement, $var_district, $var_city, $var_state, $var_country, $var_cep)
		{
			include "ConnectBD.php";
			$procedure = ("call cadastrar_cliente('". $var_cpf ."', '". $var_password ."', '". $var_login ."', '". $var_name ."', '". $var_sex ."', '". $var_email ."', str_to_date('". $var_date ."', '%d-%c-%Y'), '". $var_adress ."', '". $var_complement ."', '". $var_district ."', '". $var_city ."', '". $var_state ."', '". $var_country ."', '". $var_cep ."')");	
			$query = mysql_query($procedure, $id);
			mysql_close($id);
			if(!$query)
			{
				return false;
			}
			else
			{				
				return true;
			}
		}
		
		public static function updateClient($varidCliente, $var_cpf, $var_password, $var_login, $var_name, $var_sex, $var_email,
		$var_date, $var_adress, $var_complement, $var_district, $var_city, $var_state, $var_country, $var_cep)
		{
			include "ConnectBD.php";
			$procedure = ("call editar_cliente('". $varidCliente ."', '". $var_cpf ."', '". $var_password ."', '". $var_name ."', '". $var_sex ."', '". $var_email ."', str_to_date('". $var_date ."', '%d-%c-%Y'), '". $var_adress ."', '". $var_complement ."', '". $var_district ."', '". $var_city ."', '". $var_state ."', '". $var_country ."', '". $var_cep ."')");	
			$query = mysql_query($procedure, $id);
			mysql_close($id);
			if(!$query)
			{
				return false;
			}
			else
			{				
				return true;
			}
		}
	
		public static function GetCliente($id_cliente)
		{
			include "ConnectBD.php";
			
			$query = mysql_query("select pe.cpf, pe.nome as nome, pe.sexo,
								  cl.email, date_format(cl.data_nascimento,'%d-%m-%Y') as data_nascimentoFormated,
								  pe.senha, pe.login as login,
								  en.logradouro, en.complemento,
								  en.bairro, en.cidade,
								  en.estado, en.pais, en.cep,
								  teR.telefone as residencial,
								  teC.telefone as celular
								  from Pessoa pe 
								  inner join Cliente cl 
								  on pe.cpf = cl.cpf_cliente
								  inner join Endereco en
								  on
								  cl.id_endereco = en.id_endereco
								  inner join Telefone teR
								  on
								  cl.cpf_cliente = teR.cpf_cliente
								  inner join Telefone teC
								  on
								  cl.cpf_cliente = teC.cpf_cliente
								  where 
								  cl.cpf_cliente = '".$id_cliente. "'
								  and teR.tipo = 0
								  and teC.tipo = 1", $id);
			
			if(mysql_num_rows($query)> 0)
			{
				$client = new Client();
				$client->CPF = mysql_result($query, 0, "cpf");
				$client->Name = mysql_result($query, 0, "nome");
				$client->Gender = mysql_result($query, 0, "sexo");
				$client->Email =mysql_result($query, 0, "email");
				$client->DateOfBirth = mysql_result($query, 0, "data_nascimentoFormated");
				$client->Street = mysql_result($query, 0, "logradouro");
				$client->Complement = mysql_result($query, 0, "complemento");
				$client->District = mysql_result($query, 0, "bairro");
				$client->City = mysql_result($query, 0, "cidade");
				$client->State = mysql_result($query, 0, "estado");
				$client->Country = mysql_result($query, 0, "pais");
				$client->ZipCode = mysql_result($query, 0, "cep");
				$client->Fone = mysql_result($query, 0, "residencial");
				$client->Cel = mysql_result($query, 0, "celular");
				$client->Password = mysql_result($query, 0, "senha");
				$client->Login = mysql_result($query, 0, "login");
				
				return $client;
			}			
		}

		public static function GetAllClientes()
		{
			include "ConnectBD.php";
		
			$query = mysql_query("select pe.nome as nome, cl.email, cl.cpf_cliente as cpf from Pessoa pe inner join Cliente cl on pe.cpf = cl.cpf_cliente", $id);
		
			$AllClients = array();
			$count = 0;
		
			while($count < mysql_num_rows($query))
			{
				$client = new Client();
				$client->CPF = mysql_result($query, $count, "cpf");
				$client->Name = mysql_result($query, $count, "nome");
				$client->Email =mysql_result($query, $count, "email");
								
				$AllClients[$count] = $client;
				$count++;
			}
				
			return $AllClients;
		
		}
		
	}
?>